package {
	import we3d.Scene3d;
	import we3d.View3d;
	import we3d.filter.ZBuffer;
	import we3d.material.BitmapAttributes;
	import we3d.material.Surface;
	import we3d.primitives.Plane;
	import we3d.primitives.SimpleBox;
	import we3d.rasterizer.ScanlineTXZB;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;	

	public dynamic class WireTexture extends MovieClip {

        public var bitmap2:BitmapData;
        public var bitmap3:BitmapData;
        public var plane_attributes:BitmapAttributes;
        public var box_material:Surface;
        public var box_attributes2:BitmapAttributes;
        public var box_attributes:BitmapAttributes;
        public var plane:Plane;
        public var zraster:ScanlineTXZB;
        public var zbuffer:ZBuffer;
        public var view:View3d;
        public var box_material2:Surface;
        public var plane_material:Surface;
        public var scene:Scene3d;
        public var box2:SimpleBox;
        public var box:SimpleBox;
        public var bitmap1:BitmapData;

        public function WireTexture(){
            
            view = new View3d(550, 400);
            scene = new Scene3d();
            zbuffer = new ZBuffer(550, 400);
            view.addFilter(zbuffer);
            zraster = new ScanlineTXZB();
            zraster.zBuffer = zbuffer;
            bitmap1 = new Texture1(0x0100, 0x0100);
            bitmap2 = new Texture2(0x0100, 0x0100);
            bitmap3 = new Texture3(0x0100, 0x0100);
            plane_attributes = new BitmapAttributes(bitmap3);
            plane_material = new Surface(zraster, plane_attributes);
            plane_material.hideBackfaces = false;
            box_attributes = new BitmapAttributes(bitmap1);
            box_material = new Surface(zraster, box_attributes);
            box_attributes2 = new BitmapAttributes(bitmap2);
            box_material2 = new Surface(zraster, box_attributes2);
            plane = new Plane({surface:plane_material});
            plane.width = 250;
            plane.height = 250;
            plane.transform.x = 0;
            box = new SimpleBox({surface:box_material});
            box.transform.setRotation(((30 * Math.PI) / 180), ((40 * Math.PI) / 180), 0);
            box2 = new SimpleBox({surface:box_material2});
            box2.transform.setPosition(40, -20, 40);
            box2.transform.setRotation(((-40 * Math.PI) / 180), ((-30 * Math.PI) / 180), 0);
            scene.add(plane, box, box2);
            scene.cam.transform.setPosition(250, 200, -350);
            scene.cam.transform.setRotation(((25 * Math.PI) / 180), ((-32 * Math.PI) / 180), 0);
            scene.cam.nearClipping = 75;
            //scene.cam.nearObjCulling = -200;
            scene.invalidate();
            view.firstLayer.backToFront = false;
            view.autoClear = false;
            view.scene = scene;
            addChild(view.viewport);
            view.addEventListener(View3d.EVT_BEGIN_DRAW, beginDraw);
            view.addEventListener(View3d.EVT_END_DRAW, endDraw);
            addEventListener(Event.ENTER_FRAME, frameloop);
        }
        public function frameloop(_arg1:Event):void{
            plane.transform.rotationX = ((-((mouseY - 275)) / 400) * Math.PI);
            plane.transform.rotationY = ((-((mouseX - 200)) / 550) * Math.PI);
            scene.invalidate();
        }
        public function beginDraw(_arg1:Event):void{
            var _local2:BlurFilter;
            var _local3:BitmapData;
            _local2 = new BlurFilter(4, 4, 2);
            _local3 = view.firstLayer.bmp;
            _local3.applyFilter(_local3, _local3.rect, new Point(), _local2);
        }
        public function endDraw(_arg1:Event):void{
            var _local2:BlurFilter;
            var _local3:BitmapData;
            _local2 = new BlurFilter(2, 2, 1);
            _local3 = view.firstLayer.bmp;
            _local3.applyFilter(_local3, _local3.rect, new Point(), _local2);
        }

    }
}
